package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
	services "tinyrdm/backend/services"
	"tinyrdm/backend/types"
)

var systemLog = logs.GetLogger("System")

type SystemController struct {
	BaseController
}

type SystemReqData struct {
	Method      string   `json:"method"`
	Title       string   `json:"title"`
	DefaultName string   `json:"defaultName"`
	Extensions  []string `json:"extensions"`
}

var system = services.System()

func (c *SystemController) Handler() {
	//system.Start(c.context(), "")
	var req SystemReqData
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		logs.Error(err, string(c.Ctx.Input.RequestBody))
		c.ErrorJson(err)
		return
	}
	var resp any
	switch req.Method {
	case "Start":

		break
	case "Info":
		resp = system.Info()
		break
	case "SaveFile":
		resp = system.SaveFile(req.Title, req.DefaultName, req.Extensions)
		break
	case "SelectFile":
		resp = system.SelectFile(req.Title, req.Extensions)
		break
	default:
		resp = types.JSResp{
			Success: false,
			Msg:     "未找到该方法",
			Data:    nil,
		}
		systemLog.Printf("not found: %s", req.Method)
	}
	if resp == nil {
		resp = types.JSResp{
			Success: true,
			Msg:     "Successful",
			Data:    nil,
		}
	}
	c.postJson(resp)
}
